<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="机构" prop="departmentId">
        <el-select
          v-model="queryParams.departmentId"
          placeholder="请选择机构"
          filterable
          clearable
        >
          <el-option
            v-for="item in departmentList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="片区名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入片区名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="diy-mb-5">
      <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['admin:parkLot:create']">新增</el-button>
    </div>

    <el-table v-loading="loading" :data="spparkplaceList" >
      <el-table-column 
      v-for="(item,index) in tableList"
      :key="index"
      :label="item.label" align="center" :prop="item.prop" :width="item.width ? item.width : ''">
        <template 
        slot-scope="{row}">
          <dict-tag v-if="item.prop == 'offlineFlag' || item.prop == 'testFlag' || item.prop == 'autoSendFlag'" :options="dict.type.EYesOrNo" :value="row[item.prop]"/>
          <div v-else>{{row[item.prop]}}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="240">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            plain
            @click="handleUpdate(scope.row)"
            v-hasPermi="['admin:parkLot:update']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="danger"
            plain
            @click="handleOnline(scope.row)"
            v-hasPermi="['admin:parkLot:setOffline']"
            v-if="scope.row.offlineFlag == 0"
          >下线
          </el-button>
          <el-button
            size="mini"
            type="primary"
            plain
            @click="handleOnline(scope.row)"
            v-hasPermi="['admin:parkLot:setOnline']"
            v-if="scope.row.offlineFlag == 1"
          >上线
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车位信息对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="机构名称" prop="departmentId">
              <el-select :disabled="form.id ? true : false" v-model="form.departmentId" style="width: 100%;" placeholder="请选择机构名称" @change="handleDepartChange">
                <el-option
                  v-for="item in departmentList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="路段名称" prop="roadId">
              <el-select v-model="form.roadId"  style="width: 100%;" placeholder="请选择组织名称" >
                <el-option
                  v-for="item in roadList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="片区名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入片区名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人" prop="businessCharge">
              <el-input v-model="form.businessCharge" placeholder="请输入联系人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="businessPhone">
              <el-input v-model="form.businessPhone" placeholder="请输入联系电话"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="新能源是否单独收费" prop="newEnergyTempRuleFlag" label-width="160px">
              <el-select v-model="form.newEnergyTempRuleFlag" placeholder="请选择新能源是否单独收费" style="width:100%;">
                <el-option
                  v-for="item in dict.type.EYesOrNo"
                  :key="item.value"
                  :label="item.label"
                  :value="Number(item.value)"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="黄牌车是否单独收费" prop="yellowLicenseTempRuleFlag" label-width="160px">
              <el-select v-model="form.yellowLicenseTempRuleFlag" placeholder="请选择黄牌车是否单独收费" style="width:100%;">
                <el-option
                  v-for="item in dict.type.EYesOrNo"
                  :key="item.value"
                  :label="item.label"
                  :value="Number(item.value)"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收费规则" prop="tempRuleId">
              <el-select v-model="form.tempRuleId" placeholder="请选择" style="width:100%;">
                <el-option
                  v-for="item in tempRuleList"
                  :key="item.id"
                  :label="item.feeRuleName"
                  :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="经度" prop="longitude">
              <el-input v-model="form.longitude" placeholder="请输入经度"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="纬度" prop="latitude">
              <el-input v-model="form.latitude" placeholder="请输入纬度"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-button style="margin-left: 30px;" type="primary" @click="amapSelect">获取经纬度</el-button>
          </el-col>
          <el-col :span="12">
            <el-form-item label="详细地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入详细地址"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属位置" prop="area">
              <el-input v-model="form.area" placeholder="请输入所属位置"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="是否测试片区" prop="testFlag">
              <el-select v-model="form.testFlag" placeholder="请选择是否测试片区" style="width:100%;">
                <el-option
                  v-for="item in dict.type.EYesOrNo"
                  :key="item.value"
                  :label="item.label"
                  :value="Number(item.value)"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="是否自动派发取证工单" prop="autoSendFlag" label-width="160px">
              <el-select v-model="form.autoSendFlag" placeholder="请选择是否自动派发取证工单" style="width:100%;">
                <el-option
                  v-for="item in dict.type.EYesOrNo"
                  :key="item.value"
                  :label="item.label"
                  :value="Number(item.value)"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="!form.id">
            <el-form-item label="是否下线" prop="offlineFlag">
              <el-radio-group v-model="form.offlineFlag">
                <el-radio
                  v-for="dict in dict.type.EYesOrNo"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.newEnergyTempRuleFlag == 1">
            <el-form-item label="新能源车收费规则" prop="newEnergyTempRuleId" label-width="150px">
              <el-select v-model="form.newEnergyTempRuleId" placeholder="请选择" style="width:100%;">
                <el-option
                  v-for="item in xnyList"
                  :key="item.id"
                  :label="item.feeRuleName"
                  :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.yellowLicenseTempRuleFlag == 1">
            <el-form-item label="黄牌车收费规则" prop="yellowLicenseTempRuleId">
              <el-select v-model="form.yellowLicenseTempRuleId" placeholder="请选择" style="width:100%;">
                <el-option
                  v-for="item in hpList"
                  :key="item.id"
                  :label="item.feeRuleName"
                  :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <mapLog ref="mapLog" isDetail @mapSubmit="mapSubmit" :formType="{
      'longitude':'longitude',
      'latitude':'latitude',
      'details':'address',
    }"></mapLog>

  </div>
</template>

<script>
import {fetchList, parkLotDetail, parkLotAdd, parkLotUpdate,parkLotSetOnline,parkLotSetOffline} from "@/api/park/parkLot";
import { getDepartmentList } from '@/api/base'
import {listRoad} from "@/api/park/roadSection";
import { getTempRules } from '@/api/tempRule'
import mapLog from "@/components/mapLog";
import { validateMobile } from '@/utils/validate'
export default {
  name: "parkLot",
  dicts: ['EYesOrNo',],
  components:{mapLog},
  data() {
    return {
      tableList:[
        {label:'机构名称',prop:'departmentName',},
        {label:'路段名称',prop:'roadName',},
        {label:'片区名称',prop:'name',},
        {label:'运营负责人',prop:'businessCharge',},
        {label:'运营负责人电话',prop:'businessPhone',},
        {label:'泊位数量',prop:'berthCount',},
        {label:'是否测试片区',prop:'testFlag',},
        {label:'是否自动派发取证工单',prop:'autoSendFlag',},
        {label:'是否下线',prop:'offlineFlag',},
      ],//表格字段显示
      //片区数据
      areaList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车位信息表格数据
      spparkplaceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        departmentId: [
          {required: true, message: "机构不能为空", trigger: "change"}
        ],
        roadId: [
          {required: true, message: "路段不能为空", trigger: "change"}
        ],
        name: [
          {required: true, message: "片区名称不能为空", trigger: "blur"}
        ],
        longitude: [
          {required: true, message: "经度不能为空", trigger: "blur"}
        ],
        latitude: [
          {required: true, message: "纬度不能为空", trigger: "blur"}
        ],
        businessCharge: [
          {required: true, message: "联系人不能为空", trigger: "blur"}
        ],
        businessPhone: [
          {required: true, message: "	联系电话不能为空", trigger: "blur"},
          { validator: validateMobile.bind(this), trigger: 'blur' },
        ],
        area: [
          {required: true, message: "所属位置不能为空", trigger: "blur"}
        ],
        tempRuleId: [
          {required: true, message: "收费规则不能为空", trigger: "change"}
        ],
        newEnergyTempRuleFlag: [
          {required: true, message: "新能源是否单独收费不能为空", trigger: "change"}
        ],
        yellowLicenseTempRuleFlag: [
          {required: true, message: "黄牌车辆是否单独收费不能为空", trigger: "change"}
        ],
        newEnergyTempRuleId: [
          {required: true, message: "新能源车收费规则不能为空", trigger: "change"}
        ],
        yellowLicenseTempRuleId: [
          {required: true, message: "黄牌车收费规则不能为空", trigger: "change"}
        ],
        testFlag: [
          {required: true, message: "是否测试片区不能为空", trigger: "change"}
        ],
        autoSendFlag: [
          {required: true, message: "是否自动派发取证工单不能为空", trigger: "change"}
        ],
        offlineFlag: [
          {required: true, message: "是否下线不能为空", trigger: "change"}
        ],
      },
      departmentList: [],//机构数据
      roadList:[],//路段数据
      tempRuleList: [],//普通临时车收费规则数据
      xnyList:[],//新能源收费规则数据
      hpList:[],//黄牌收费规则数据
    };
  },
  created() {
    this.getDepartmentList();
    this.getList();
  },
  methods: {
    //地图打开
    amapSelect(){
      this.$refs.mapLog.mapLoad(this.form)
    },
    mapSubmit(form){//地图弹窗提交
      this.form = {...form}
      console.log('111',this.form)
    },
    getTempRules(departmentId, type) {//获取收费规则
      getTempRules({ departmentId, type }).then(response => {
        let data = response.data || []
        if (type == 1) this.tempRuleList = data
        if (type == 2) this.xnyList = data
        if(type == 4) this.hpList = data
      })
    },
    getDepartmentList(){//获取机构
      getDepartmentList().then(res => {
        this.departmentList = res.data || []
      })
    },
    async listRoad(id){//根据机构获取组织
			let res = await listRoad(id)
			let list = res.data || []
			this.roadList = [...list]
		},
    handleDepartChange(val) {//机构选择
      this.roadList = []
			let form = {...this.form}
			form.organizationId = ''
      form.tempRuleId = ''
      form.newEnergyTempRuleId = ''
      form.yellowLicenseTempRuleId = ''
			this.form = {...form}
      if(!val) return this.roadList = []
			this.listRoad(val)
      this.getTempRules(val, 1)
      this.getTempRules(val, 2)
      this.getTempRules(val, 4)
    },
    //下线上线
    handleOnline(row) {
      let api = row.offlineFlag == 1 ? parkLotSetOnline : parkLotSetOffline
      this.$modal.confirm(`是否确认${row.offlineFlag == 1 ? '上线' : '下线'}该数据项？`).then(function() {
        return api(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess('操作成功');
      }).catch(() => {});
    },
    /** 查询车位信息列表 */
    getList() {
      this.loading = true;
      fetchList(this.queryParams).then(response => {
        let data = response.data || {}
        let rows = data.list || []
        this.spparkplaceList = rows.map(item=>{
          let parkNameList = item.parkNameList || []
          return {
            ...item,
            parkNameStr:parkNameList.toString()
          }
        });
        this.total = data.total || 0;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form = {};
      this.open = true;
      this.title = "添加路段信息";
      this.getDepartmentList()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const pkParkplaceId = row.id
      parkLotDetail(pkParkplaceId).then(response => {
        this.form = response.data || {};
        this.open = true;
        this.title = "修改路段信息";
        this.listRoad(this.form.departmentId)
        this.getDepartmentList()
        this.getTempRules(this.form.departmentId, 1)
        this.getTempRules(this.form.departmentId, 2)
        this.getTempRules(this.form.departmentId, 4)
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            parkLotUpdate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            parkLotAdd(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>
<style lang="scss">
 .amap-icon img,.amap-icon,
.amap-marker-content img{
    width: 25px!important;
    height: 34px!important;
}
#container {width:650px; height: 300px; }
</style>
